class GraphLine extends Graph2D
{
  ArrayList SelectedSites;
  ArrayList DataList; 
  HashMap siteInfo;
  int maxValGlobal;

  GTimeLine gtl ;

  DropBox dbox1, dbox2, dbox3;
  ArrayList timesSite01, timesSite02, timesSite03;

  GraphLine (HashMap data, HashMap siteInfo, int X, int Y, int W, int H)
  {
    /** Initialize the Base Class **/
    super (X, Y, W, H);

    DataList = new ArrayList(data.values());

    this.siteInfo = siteInfo;

    Date d = ((SitesByTime)DataList.get(0)).date;

    gtl = new GTimeLine(d, d, d);

    SelectedSites = new ArrayList();

    dbox1 = new DropBox(20,100,350);
    dbox2 = new DropBox(20,280,350);
    dbox3 = new DropBox(20,460,350);

    timesSite01 = new ArrayList();
    timesSite02 = new ArrayList();
    timesSite03 = new ArrayList();
    
    maxValGlobal = 1;
  }

  void draw()
  {
    gtl.draw();

    dbox1.render();
    dbox2.render();
    dbox3.render();

    //println("dbox1:"+dbox1.getSelectedString());
    //println("dbox2:"+dbox2.getSelectedString());
    //println("dbox3:"+dbox3.getSelectedString());

    if( !dbox1.getSelectedString().equals("") )
      timesSite01 = ((WebSiteInfo)siteInfo.get( dbox1.getSelectedString() )).timeStampList;

    if( !dbox2.getSelectedString().equals("") )  
      timesSite02 = ((WebSiteInfo)siteInfo.get( dbox2.getSelectedString() )).timeStampList;

    if( !dbox3.getSelectedString().equals("") )  
      timesSite03 = ((WebSiteInfo)siteInfo.get( dbox3.getSelectedString() )).timeStampList;

    maxValGlobal = 1;

    findMaxVal(timesSite01, timesSite02, timesSite03);

    if(timesSite01.size() > 0)
      Plot(timesSite01, 240);

    if(timesSite02.size() > 0)
      Plot(timesSite02, 420);

    if(timesSite03.size() > 0)
      Plot(timesSite03, 600);


  }

  Date parseDate(String strz)
  {
    String[] s = split(strz, " ");
    String[] dates = split(s[0],"/");
    return new Date(Integer.parseInt(dates[2])-1900, Integer.parseInt(dates[0])-1, Integer.parseInt(dates[1]));
  }

  void findMaxVal(ArrayList timesSite01, ArrayList timesSite02, ArrayList timesSite03){
    int maxVal01 = 0; 
    if(timesSite01.size() > 0){
      Date pdate01 = parseDate((String)timesSite01.get(0)); 

      int totalCount01 = 0;  
      for(int i = 0; i < timesSite01.size(); i ++)
      {
        Date date = parseDate((String)timesSite01.get(i));

        totalCount01 +=  1;
        if(pdate01.getDate() != date.getDate())
        {
          if(totalCount01 > maxVal01)
            maxVal01 = totalCount01;
          totalCount01 = 0;
        }
        pdate01 = parseDate((String)timesSite01.get(i));
      }
    }
    //println("01"+maxVal01);    
  
    int maxVal02 = 0; 
    if(timesSite02.size() > 0){
      Date pdate02 = parseDate((String)timesSite02.get(0)); 

      int totalCount02 = 0;  
      for(int i = 0; i < timesSite02.size(); i ++)
      {
        Date date = parseDate((String)timesSite02.get(i));

        totalCount02 +=  1;
        if(pdate02.getDate() != date.getDate())
        {
          if(totalCount02 > maxVal02)
            maxVal02 = totalCount02;
          totalCount02 = 0;
        }
        pdate02 = parseDate((String)timesSite02.get(i));
      }
    }
    //println("02"+maxVal02);  
    
    int maxVal03 = 0; 
    if(timesSite03.size() > 0){
      Date pdate03 = parseDate((String)timesSite03.get(0)); 

      int totalCount03 = 0;  
      for(int i = 0; i < timesSite03.size(); i ++)
      {
        Date date = parseDate((String)timesSite03.get(i));

        totalCount03 +=  1;
        if(pdate03.getDate() != date.getDate())
        {
          if(totalCount03 > maxVal03)
            maxVal03 = totalCount03;
          totalCount03 = 0;
        }
        pdate03 = parseDate((String)timesSite03.get(i));
      }
    }
    //println("03"+maxVal03); 
    int arr[] = {maxVal01, maxVal02, maxVal03, 1};
    maxValGlobal = max(arr);
  }

  void Plot(ArrayList DataList, int y)
  {
    int totalCount = 0;
    
    pushStyle();
    fill(255);
    text(0,5,y+10);
    text(maxValGlobal, 5, y-120);
    popStyle();
  
    //println(maxValGlobal);
    Date pdate = parseDate((String)DataList.get(0));
    for(int i = 0; i < DataList.size(); i ++)
    {
      Date date = parseDate((String)DataList.get(i));

      totalCount +=  1;
      if(pdate.getDate() != date.getDate())
      {
        fill(0xFFFFFF00);
        rect(gtl.getX(pdate), y, 10, (-totalCount*140)/maxValGlobal);
        totalCount = 0;
      }
      pdate = parseDate((String)DataList.get(i));
    }
    fill(0xFFFFFF00);
    //println();
    rect(gtl.getX(pdate), y, 10, (-totalCount*140)/maxValGlobal);

  }
  void mouseDragged()
  {
    gtl.mouseDragged();
  }

  void mouseOver()
  {
    gtl.mouseMoved();
  }

  void mousePressed()
  {
    gtl.mousePressed();
  }

  void mouseWheel(int delta)
  {
    gtl.mouseWheel(delta);
  }

  void mouseClicked()
  {
    dbox1.processClick();
    dbox2.processClick();
    dbox3.processClick();
  }

  void keyPressed() 
  {
    dbox1.processKey();
    dbox2.processKey();
    dbox3.processKey();
  }
}



